package com.jsmile.cloud.accountcenter.shop.repo;

import java.util.Objects;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jsmile.cloud.accountcenter.api.req.shop.ReqShopQuery;
import com.jsmile.cloud.accountcenter.shop.dao.ShopDao;
import com.jsmile.cloud.accountcenter.shop.model.Shop;

import lombok.extern.slf4j.Slf4j;

/**
 * @author 龚亮
 * @version 1.0
 * @desc 为了薪资破万，还是睡一会儿吧...!
 * @date 2020年06月04日 04:39:24
 */
@Service
@Slf4j
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class ShopSearchRepository extends ServiceImpl<ShopDao, Shop> {

    public Shop findOneByUserId(Long userId) {
        return this.baseMapper.findOneByUserId(userId);
    }

    public IPage<Shop> pageQuery(ReqShopQuery reqShopQuery) {
        return this.baseMapper.selectPage(reqShopQuery.getPage(), getQueryWrapper(reqShopQuery));
    }

    private Wrapper getQueryWrapper(ReqShopQuery reqShopQuery) {
        return new QueryWrapper<Shop>().lambda().eq(null != reqShopQuery.getEnable(), Shop::getEnable, reqShopQuery.getEnable())
            .eq(null != reqShopQuery.getCurTenantId(), Shop::getTenantId, reqShopQuery.getCurTenantId()).eq(null != reqShopQuery.getShopId(), Shop::getId, reqShopQuery.getShopId())
            .like(Objects.nonNull(reqShopQuery.getShopNo()), Shop::getShopNo, reqShopQuery.getShopNo())
            .like(Objects.nonNull(reqShopQuery.getContactName()), Shop::getContactName, reqShopQuery.getContactName())
            .like(Objects.nonNull(reqShopQuery.getShopName()), Shop::getShopName, reqShopQuery.getShopName())
            .like(Objects.nonNull(reqShopQuery.getContactPhone()), Shop::getContactPhone, reqShopQuery.getContactPhone())
            .ge(null != reqShopQuery.getStartDate(), Shop::getCreateTime, reqShopQuery.getStartDate())
            .le(null != reqShopQuery.getEndDate(), Shop::getCreateTime, reqShopQuery.getEndDate()).orderByDesc(Shop::getCreateTime);// sort,createTime倒序
    }
}
